﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sharp.Infrastructure.CommandLineParser;
using Sharp.Infrastructure.CommandLineParser.Arguments;
using Sharp.Infrastructure.CommandLineParser.Exceptions;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;

namespace DevTools
{
    class Program
    {
        static void Main(string[] args)
        {
            CommandLineParser parser = CreateParser();

            try
            {
                parser.ParseCommandLine(args);
            }
            catch (CommandLineException e)
            {
                Console.WriteLine(e.Message);
                parser.ShowUsage();
                Console.ReadKey();
                return;
            }


            Console.WriteLine("正在执行...");
            DateTime start = DateTime.Now;

            var rebuild = ((SwitchArgument)parser.LookupArgument("rebuild"));

            if (rebuild.Value)
            {
                PermissionScan ps = new PermissionScan();
                var list = ps.GetAllActionByAssembly().Distinct(new PermissionComparer()).ToList();
                 

                using (var proxy = new ServiceProxy<IUserService>())
                {
                    proxy.Channel.UpdatePermission(list);
                }
            }

          
            Console.WriteLine("完成...");
             

            Console.WriteLine("耗时: " + (DateTime.Now - start).TotalSeconds + "秒");
            Console.ReadKey();
        }

        private static CommandLineParser CreateParser()
        {
            CommandLineParser parser = new CommandLineParser();
            var rebuildArgument = new SwitchArgument('p', "rebuild",false)
            {
                Optional = true
            };

         

      
            FileArgument helpArgument = new FileArgument('?', "help", "帮助")
            {
                FileMustExist = false,
                Optional = true
            };

            parser.Arguments.Add(rebuildArgument); 
            parser.Arguments.Add(helpArgument);

            parser.IgnoreCase = true;
            return parser;
        }
    }
}
